#include "conf.hpp"
#include <fstream>
#include <sstream>

conf* conf::instance()
{

	static conf * single = new conf();
	return single;
}


conf::conf()
{
	conf_vec_ = {

		{"common","edition"},
		//{"common","mcu_ip"},

		{"common","user"},
		{"common","pwd"},
		
		/*{"v1","use_cucm"},
		{"v1","current_mcu"},
		{"v1","up_server"},

		{"v2","main_ip"},
		{"v2","back_ip"},
		{"v2","cucm_ip"},
		{"v2","back_cucm_ip"},

		{"v2","use_cucm"},
		{"v2","current_mcu"},
		{"v2","up_server"},*/
		
		

	};
}


bool conf::init(string config_path)
{
	if (initalize_)
	{
		return false;
	}

	ifstream file_tmp;
	stringstream buffer;
	string contents;
	int ret;

	

	auto print_err = [&]() {
		cout << ini.GetErrMsg().c_str() << endl;
	};

	if (ini.Load(config_path) == ERR_OPEN_FILE_FAILED) {
		print_err();
		return false;
	}
	for (auto it : conf_vec_)
	{
		if (ini.GetStringValue(it.section, it.name, &it.data) != RET_OK)
		{
			print_err();
			false;
		}
		conf_map_.emplace(it.section+"/"+it.name, it);
	}

	
	initalize_ = true;
	return true;

}

void conf::write(const string& section, const string& key, const string& value)
{
	ini.SetStringValue(section, key, value);
	ini.Save();
}
